/**
 * 全局访问路径
 */
var path = "http://127.0.0.1:8899/api/consumer/"
/**
 * 图片全局路径
 */
var imgs = "http://120.48.47.144:8899/images/";
/**
 * 页面全局路径
 */
var pathHtml = "http://127.0.0.1:8899/html/";
/**
 * 
 */
var information_id;   //资讯id
var car_series_id;   //车系id
$(function() {
	/* 根据token获取用户信息 */
	get_user_mess_and_token();
	//自己代码
	//获取所有资讯传过来的id
	var url = location.search; //路径   location.search.substr(1).split('/').indexOf(1)
	var url_x_index = url.indexOf('/'); /* 斜杠下标 */
	var url_x_last_index = url.lastIndexOf('//'); /* 最后参数斜杠下标 */
	information_id= url.substring(url.indexOf('?')+1,(url_x_index != -1 ? url_x_index : url.length));  //获取第一个参数
	car_series_id=url.substring(url_x_index+1,(url_x_last_index != -1 ? url_x_last_index : url.length));  //获取第二个参数
	//查询单个资讯
	singleInformation();
	//根据车系id查询此系列的所有资讯(相关推荐)
	if(information_id==0){
		$("#jsx_new").css("display","none");
		$("#detailsZiXun").css("height","600px");
		$("#detailsZiXun article div").remove();
		var str="";
		str+="<div style='left:50%;top:50%;transform:translate(-50%, -50%)' class='jsx-4111927491 no-page-wrap tw-absolute tw-text-center height: 300px;'><img src='//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/empty-page-icon-1-9bad398ed15dd2de5087..png' class='jsx-4111927491 tw-object-center tw-object-cover'>"+
			"<span>"+
				"<p>此车暂无资讯信息</p>"+
				"<p class='jsx-4111927491 no-page-btn'><a href='"+pathHtml+"car_information.html"+"'>看资讯</a></p>"+
			"</span>"+
		"</div>"
		$("#detailsZiXun article").append(str);
	}
	if(car_series_id==0){
		$("#jsx_new").css("display","none");
	}
	carSeriesInformation();
	//回到顶部
	backVertex();
	
});

//**************查询单个资讯信息**************************

function singleInformation() {
	$.post(path + 'InformationController/singleInformation/' +information_id, function(data) {
		// //时间变量
		// var information_release_date = "";
		// //当前时间
		// var time = new Date();
		// var newDate = "" + time.getFullYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() +
		// 	"";
		// //转换统一时间格式方便计算
		// var date = new Date(newDate);
		// var modify = new Date(data.information_release_date);
		// /**
		//  * 如果小于七天，数字表示，
		//  * 否则使用后台返回的时间数据
		//  */
		// if (parseInt(date - modify) / (1000 * 60 * 60 * 24) <= 20) {
		// 	information_release_date = parseInt((date - modify) / (1000 * 60 * 60 * 24)) + "天前";
		// } else {
		// 	information_release_date = data.information_release_date;
		// }
		//时间变量
		var information_release_date = "";
		var date1 = new Date(data.information_release_date); //开始时间
		var date2 = new Date(); //结束时间
		var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数
		//alert(date3);
		var days = Math.floor(date3 / (24 * 3600 * 1000)); // 计算出相差天数
		//计算出小时数
		var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
		var hours = Math.floor(leave1 / (3600 * 1000));
		//计算出相差分钟数
		var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
		var minutes = Math.floor(leave2 / (60 * 1000));
		if (days != 0 && days <= 7) {
			information_release_date = days + "天前";
		} else if (days == 0 && hours != 0) {
			information_release_date = hours + "小时前";
		} else if (days == 0 && hours == 0 && minutes != 0) {
			information_release_date = minutes + "分钟前";
		} else {
			information_release_date = data.information_release_date;
		}
		var str="<article class='jsx-2433290478'>"+
					"<div class='jsx-1513769121 details'>"+
						"<h1 class='jsx-1513769121 title'>"+data.information_title+"</h1>"+
							"<div data-log-click='{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;content_user_profile&quot;,&quot;to_user_id&quot;:&quot;2825095518&quot;}}' class='jsx-1513769121 info'>"+
							 "<div style='margin-right:8px' class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>"+
							 "<div style='display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0' class='avatar tw-rounded-full'>"+
							 "<div style='box-sizing:border-box;display:block;max-width:100%'>"+
							 "<img style='max-width:100%;display:block;margin:0;border:none;padding:0' alt='' src='data:image/svg+xml;utf-8,&lt;svg width=&quot;24&quot; height=&quot;24&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/&gt;' aria-hidden='true' role='presentation' />"+
							 "</div>"+
							 "<noscript>"+
								"<img src='//p3.dcarimg.com/img/pgc-image/e42de2e6be024abfb899771ebc9bc677~48x0.image' alt='"+data.information_title+"头像' width='24' height='24'"+
								"style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class='' />"+
							 "</noscript>"+
							"<img src='" + imgs + data.user.user_head_portrait_url +"' "+
							"alt='"+data.user.user_name+"头像' width='24' height='24' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class='' />"+
							"</div>"+
							"</div><a href='"+pathHtml+"car_my_homepage.html?"+data.information_release_user_fk_id+"' target='_blank' class='jsx-1513769121 source g-active-link-text' rel='opener'>"+data.user.user_name+"</a>"+
							"<span class='jsx-1513769121 time'>"+information_release_date+"</span>"
							if(user_id==data.information_release_user_fk_id){
								str+=""
							}else{
								str+="<button type='button' class='jsx-1824960999 btn' onclick='insert_or_delete_follow_user()' id='follow' value='"+data.information_release_user_fk_id+"'>关注</button>"+
							"<button type='button' class='jsx-1824960999 btn follow' onclick='insert_or_delete_follow_user()' style='display: none;' id='followed' value='"+data.information_release_user_fk_id+"'>已关注</button>"
							}
							str+="</div>"+
							"</div>"+
							"<div class='jsx-2433290478'>"+
							"<div class='jsx-3371063651 details'>"+
							"<section id='article' class='jsx-3371063651 article-content'>"+
							"<p>"+data.information_content.information_content_1+"</p>"+
							"<img src='" + imgs + data.information_images.information_images_1 +"'>"+
							"<p>"+data.information_content.information_content_2+"</p>"+
							"<img src='" + imgs + data.information_images.information_images_2 +"'>"+
							"<p>"+data.information_content.information_content_3+"</p>"+
							"<img src='" + imgs + data.information_images.information_images_3 +"'>"+
							"<p>"+data.information_content.information_content_4+"</p>"+
							"<img src='" + imgs + data.information_images.information_images_4 +"'>"+
							"<p>"+data.information_content.information_content_5+"</p>"+
							"</section>"+
							"</div>"+
							"</div>"+
							"<div class='jsx-1671432716 article-bottom'>"+
								"<div class='jsx-1671432716'>"+
									"<div class='jsx-1671432716 stats-wrap'>"+
										"<div class='jsx-1671432716 read-count font-RPP4MYtWOvsBmV80 tw-text-14 tw-leading-24 tw-mr-25'>" + (data.information_read_count < 10000 ? data.information_read_count : (data.information_read_count / 10000).toFixed(2) + "万") +"<!-- -->人阅读"+
										"</div>"+
										"<div class='tw-flex tw-items-center' id='guanzhu'>"+
											"<div class='tw-cursor-pointer hover:tw-text-common-orange tw-text-video-gery tw-text-14 tw-mr-25' "+
											"style='display:block' onclick='addCollection()' value='"+data.user.user_id+"' id='collection'>"+
											"<i class='DCD_Icon icon_collect_24_normal tw-text-14 tw-mr-3'></i><span>收藏</span>"+
											"</div>"+
											"<div class='tw-cursor-pointer hover:tw-text-common-orange tw-text-video-gery tw-text-14 tw-mr-25' "+
											"style='display:none' onclick='addCollection()' value='"+data.user.user_id+"' id='collections'>"+
											"<i class='DCD_Icon icon_collect_select_pc tw-text-common-yellow tw-text-14 tw-mr-3'></i><span>已收藏</span>"+
											"</div>"+
										"</div>"+
									"</div>"+
								"</div>"+
							"</div>"+
						"</article>";
		$("#detailsZiXun article").remove();
		$("#detailsZiXun").append(str);
		if (user_id != 0) //已登录
			select_user_whether_follow_user(); /* 查询用户是否已关注该车系 */
		/* 查询用户是否已关注该资讯*/
		if(user_id!=0)
			select_user_collection_infomation();
		
	},"json");
	
}

/* 查询用户是否已关注该用户 */
function select_user_whether_follow_user() {
	var cover_user_id=$("#guanzhu div").attr("value");
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/Follow_userController/select_user_follow_user",
		type: "post",
		data: JSON.stringify({
			"follow_user": {
				"follow_user_fk_id": user_id,
				"follow_cover_user_fk_id": cover_user_id
			}
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				$("#follow").hide();
				$("#followed").show();
			}
		}
	});
}

/* 查询用户是否已收藏该资讯 */
function select_user_collection_infomation(){
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/collection_informationController/select_user_collection_infomation",
		type: "post",
		data: JSON.stringify({
			"collection_information": {
				"collection_information_user_fk_id": user_id,
				"collection_information_fk_id": information_id
			}
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				$("#collection").hide();
				$("#collections").show();
			}
		}
	});
}

//添加收藏和取消收藏
function addCollection(){
	if (user_id == 0) { //未登录
		location.href = "http://127.0.0.1:8899/html/car_login.html?car_information_details/"+information_id+"/"+car_series_id;
		return;
	}
	var type = $("#collections").css("display") == "none" ? "insert" : "delete";
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/collection_informationController/insert_or_delete_collection_information/",
		type: "post",
		data: JSON.stringify({
			"collection_information": {
				"collection_information_user_fk_id": user_id,
				"collection_information_fk_id": information_id
			},
			"type": type
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "JSON",
		success: function(data) {
			if (data.result == "true") {
				if (data.result == "true") {
					if (type == "insert") {
						$("#collection").hide();
						$("#collections").show();
					} else if(type=="delete"){
						$("#collection").show();
						$("#collections").hide();
					}
				}
			}
		}
	});
}

/* 关注/取消关注 */
function insert_or_delete_follow_user() {
	if (user_id == 0) { //未登录
		location.href = "http://127.0.0.1:8899/html/car_login.html?car_information_details/"+information_id+"/"+car_series_id;
		return;
	}
	var cover_user_id=$("#guanzhu div").attr("value");
	var type = $("#followed").css("display") == "none" ? "insert" : "delete";
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/Follow_userController/insert_or_delete_follow_user_id",
		type: "post",
		data: JSON.stringify({
			"follow_user": {
				"follow_user_fk_id": user_id,
				"follow_cover_user_fk_id": cover_user_id
			},
			"type": type
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				if (type == "insert") {
					$("#follow").hide();
					$("#followed").show();
				} else if(type=="delete"){
					$("#follow").show();
					$("#followed").hide();
				}
			}
		}
	});
}


//**************查询单个资讯信息(相关推荐)**************************
function carSeriesInformation(){
	$.post(path+'InformationController/seleCarSeriesInformation/'+car_series_id,function(data){
		var str="";
		$(data).each(function(index,item){
				str+="<div class='jsx-1719232559 tw-h-98 xl:tw-h-102 border'>"+
				"<div class='jsx-3847382121 feed-item tw-h-full '>"+
				"<div class='jsx-3847382121 image-wrap tw-w-90 xl:tw-w-108 tw-h-60 xl:tw-h-72 tw-relative'>"+
				"<a data-log-click='{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;related_group_module_item_group&quot;}}' href='"+pathHtml+"car_information_details.html?"+item.information_id+"/"+item.car_model.car_series.car_series_id+"' target='_self' class='jsx-3847382121 image-link ' rel='opener'>"+
				"<div class='tw-rounded-2 g-load-img-wrap' style='display: block; overflow: hidden; position: absolute; inset: 0px; z-index: 0;'>"+
				"<img src='"+imgs + item.information_images.information_images_1+"' alt='"+item.information_title+"' width='133' height='72' class='zoom' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>"+
				"</div>"+
				"</a>"+
				"</div>"+
				"<div class='jsx-3847382121 info-right tw-ml-10 '>"+
				"<a data-log-click='{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;related_group_module_item_group&quot;}}' target='_self' href='"+pathHtml+"car_information_details.html?"+item.information_id+"/"+item.car_series_id+"' "+
				" title='"+item.information_title+"' class='jsx-3847382121 title line-2 g-active-link-text tw-text-14 xl:tw-text-16 tw-font-semibold' rel='opener' style='width:230px;position:relative;left:105px;top:-70px'>"+item.information_title+"</a>"+
				"<div class='jsx-3847382121 info-bottom' style='width:230px;position:relative;left:105px;top:-70px;'>"+
				"<a data-log-click='{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;related_group_module_item_user&quot;,&quot;to_user_id&quot;:&quot;3773938935274909&quot;}}' target='_blank' href='"+pathHtml+"car_my_homepage.html?"+item.information_release_user_fk_id+"' class='jsx-3847382121 author g-active-link-text line-1 md:tw-inline-block tw-hidden tw-mt-2' rel='opener'>"+item.user.user_name+"</a>"
				if((item.user.user_name).length>5){
					str+="<span class='jsx-3847382121 play-count line-1 tw-mt-2' style='position:relative;left:36px;top:-5px;'>" + (item.information_read_count < 10000 ? item.information_read_count : (item.information_read_count / 10000).toFixed(2) + "万") +"次阅读</span>"+
					"</div>"+
					"</div>"+
					"</div>"+
					"</div>"
				}else if((item.user.user_name).length<=5 && (item.user.user_name).length>=4){
					str+="<span class='jsx-3847382121 play-count line-1 tw-mt-2' style='position:relative;left:85px;top:-5px;'>" + (item.information_read_count < 10000 ? item.information_read_count : (item.information_read_count / 10000).toFixed(2) + "万") +"次阅读</span>"+
					"</div>"+
					"</div>"+
					"</div>"+
					"</div>"
				}
				else{
					str+="<span class='jsx-3847382121 play-count line-1 tw-mt-2' style='position:relative;left:105px;top:-5px;'>" + (item.information_read_count < 10000 ? item.information_read_count : (item.information_read_count / 10000).toFixed(2) + "万") +"次阅读</span>"+
					"</div>"+
					"</div>"+
					"</div>"+
					"</div>"
				}
		});
		$("#series_zixun div").remove();
		$("#series_zixun").append(str);
	},"json");
	
}

/* 回到顶点 */
function backVertex(windowTop){
	//向上移动到0px
	$("#gotoTop").click(function(){
		$('html,body').animate({scrollTop:'0px'},'slow');
	});
	
	//获取最小高度，无法入值则默认600
	windowTop?windowTop=windowTop:windowTop=600;
	
	//为窗口的scroll事件绑定函数
	$(window).scroll(function(){
		//获取当前高度 
		var height=$(window).scrollTop();
		if(height>windowTop){
			$("#gotoTop").fadeIn(500);//500毫秒速度
		}else{
			$("#gotoTop").fadeOut(500);
		}
	})
}

